<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .adver {
            width: 300px;
            /* height: 400px; */
            background-color: aquamarine;
        }

        .adver-header {
            overflow: hidden;
            line-height: 20px;
            font-size: 13px;
            padding: 0 5px;
        }

        .adver-header .adver-title {
            float: left;
        }

        .adver-header .adver-close {
            float: right;
        }

        .adver-main img {
            width: 100%;
        }

        .adver-1{
            position: fixed;
            right: 5px;
            bottom: 5px;
        }

        .adver-2{
            position: fixed;
            right: 5px;
            top: 5px;
        }
        
    </style>
    <script src="../js/vue.js"></script>
</head>
<body>
    <div id="app">
          <!-- 父组件使用子组件 (调用组件) -->
        <laowang-adver class="adver-1" :interval="10" :delay="5000" :is-loop="true" img="../images/bzsc-dgbhz-300-250-7-5.gif"></laowang-adver>
        <laowang-adver class="adver-2"  :interval="5"  :delay="2000" :is-loop="true" img="https://img13.360buyimg.com/seckillcms/s280x280_jfs/t20270319/157729/12/43682/19693/65f945c4Fa3569480/1a3524b43a44cffb.png.avif" ></laowang-adver>
    </div>
</body>
<script>
    /* 
        小广告
            (1) 页面加载完毕 等待5s 小广告显示
            (2) 倒计时10s后关闭小广告
            (3) 重复1-2
            (4) 点击关闭按钮 => 清除小广告 => 重复 1 2 3
    
    */



    // 局部组件 (封装组件)
    let LaowangAdver = {
        // props: ["interval","delay","isLoop","img"],
        props: {
            interval:{
                type:Number,
                default:10,
            },
            delay:{
                type:Number,
                default:5000,
            },
            isLoop:{
                type:Boolean,
                default:false,
            },
            img:{
                type:String,
                required:true,
            }
        },
        data(){
            return {
                // 将父组件传入的数据作为子组件的初始值
                countDown: this.interval,
                delayTime: this.delay,
                loop: this.isLoop, // 是否重复显示小广告

                isShow:false,
                countDownTimer:null,
            }
        },
        template:`<div class="adver" v-if="isShow">
            <div class="adver-header">
                <div class="adver-title">距离广告关闭还有{{countDown}}秒</div>
                <div class="adver-close" @click="closeHandler">X</div>
            </div>
            <div class="adver-main">
                <img :src="img" alt="">
            </div>
        </div>`,
       
        methods:{
            showAdver(){
                // 等待5s 小广告显示
                setTimeout(()=>{
                    this.isShow = true;

                    // 倒计时10s后关闭小广告
                    this.countDownTimer = setInterval(()=>{
                        this.countDown--;
                        console.log(1111, this.countDown);
                        if(this.countDown == 0){
                            this.isShow = false; // 关闭广告
                            clearInterval(this.countDownTimer);

                            this.countDown = 5;

                            if(this.loop){
                                this.showAdver();
                            }
                        }
                    },1000);
                },this.delayTime)
            },
            closeHandler(){
                this.isShow = false; // 关闭广告
                clearInterval(this.countDownTimer);

                if(this.loop){
                    this.showAdver();
                }
            }
        },
        computed:{
            
        },
        mounted () {
            console.log(this);
            this.showAdver();
        },
        beforeDestroy () {

        }
    }





    var vm = new Vue({
        el:"#app",
        data:{
            
        },
        components: {
            LaowangAdver,  
        },

    })


    


</script>
</html>